#include "app_common.h"
#include "sys/time.h"
#include "stdio.h"

long app_common_getCurrentTime(){
    struct timeval tv;
    gettimeofday(&tv, NULL);
    return tv.tv_sec * 1000 + tv.tv_usec / 1000;
}

/**
 * @brief 字符串转16进制
 * 
 * @param str  字符串
 * @param len 字符串长度
 * @return char* 16进制字符串
 */
char *app_common_charsToHex(char *str, int len){
    char *hex = (char *)malloc(len * 2 + 1);
    for (int i = 0; i < len; i++){
        sprintf(hex + i * 2, "%02x", str[i]);
    }
    hex[len * 2] = '\0';
    return hex;
}

/**
 * @brief 16进制转字符串
 * 
 * @param hex 16进制字符串
 * @param len 字符串长度
 * @return char*    字符串
 */
char *app_common_hexToChars(char *hex, int *chars_len){
    int len = strlen(hex);
    char *str = (char *)malloc(len/2);
    for (int i = 0; i < len; i += 2){
        sscanf(hex + i, "%2X",(unsigned int *)(str + i/2));
    }
    *chars_len = len/2;
    return str;
}